package com.rongshu.view.home.adapter.pageradapter;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.rongshu.utils.Constant;
import com.rongshu.view.home.entity.ArticleItem;
import com.rongshu.view.home.fragment.child.ArticleDetailsFragment;

import java.util.List;

public class ArticleDetailsPagerAdapter extends FragmentStatePagerAdapter {

    private List<ArticleItem> responseBeanList = null;
    private boolean is_position_none = false;

    public void setResponseBeanList(List<ArticleItem> list) {
        this.responseBeanList = list;
    }

    public List<ArticleItem> getResponseBeanList() {
        return responseBeanList;
    }

    public ArticleDetailsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return createForNoExpand(position);
    }

    @Override
    public int getCount() {
        return responseBeanList == null ? 0 : responseBeanList.size();
    }

    /**
     * 解决重新找上一个fragment
     *
     * @return
     */
    @Override
    public Parcelable saveState() {
        return new Bundle();
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return is_position_none ? POSITION_NONE : super.getItemPosition(object);
    }

    /**
     * 外部通知刷新方法
     */
    public void notifyDataSetChanged(boolean is_position_none) {
        this.is_position_none = is_position_none;
        super.notifyDataSetChanged();
    }

    private Fragment createForNoExpand(int position) {
        Fragment fragment = null;
        fragment = new ArticleDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constant.INTENT_DATA, responseBeanList.get(position));
        fragment.setArguments(bundle);
        return fragment;
    }
}
